﻿#include <iostream>
#include <string>
#include <unordered_map>

bool isPrime(int n)
{
    if (n <= 1)
    {
        return false;
    }
    for (int i = 2; i * i <= n; i++)
    {
        if (n % i == 0)
        {
            return false;
        }
    }
    return true;
}

std::string findNearestPrime(std::string w1, std::string w2, std::string w3)
{
    std::unordered_map < std::string, std::string> chisla = {
        { "zero", "0"}, { "one", "1"}, { "two", "2"}, { "three", "3"}, { "four", "4"},
        { "five", "5"}, { "six", "6"}, { "seven", "7"}, { "eight", "8"}, { "nine", "9"}
    };

    std::string num_str = chisla[w1] + chisla[w2] + chisla[w3];
    if (!std::all_of(num_str.begin(), num_str.end(), ::isdigit) || num_str.length() != 3)
    {
        return "Wrong input";
    }

    int num = std::stoi(num_str);
    int l = num - 1;
    int u = num + 1;

    while (true)
    {
        if (isPrime(l))
        {
            return std::to_string(l);
        }
        else if (isPrime(u))
        {
            return std::to_string(u);
        }
        else
        {
            l--;
            u++;
        }
    }
}

int main()
{
    std::string w1, w2, w3;
    std::cout << "Введите числа: ";
    std::cin >> w1 >> w2 >> w3;

    std::string result = findNearestPrime(w1, w2, w3);
    std::cout << result << std::endl;

    return 0;
}